package com.qf.controller;

import com.alibaba.druid.util.StringUtils;
import com.qf.pojo.*;
import com.qf.service.*;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import com.qf.vo.GoodsAllinone;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.ws.Service;
import java.rmi.MarshalledObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/goods")
public class AdminGoodsController {
    @Autowired
    BrandService brandService;
    @Autowired
    GoodsService goodsService;
    @Autowired
    DtsGoodsAttributeService attributeService;
    @Autowired
    DtsGoodsSpecificationService specificationService;
    @Autowired
    DtsGoodsProductService productService;
    @RequestMapping("/update")
    public Object updateGoods(@RequestBody GoodsAllinone goodsAllinone){
        DtsGoods goods = goodsAllinone.getGoods();

        DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();

        DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();

        DtsGoodsProduct[] products = goodsAllinone.getProducts();

        goodsService.updateGoods(goods);

        specificationService.update(specifications);

        attributeService.update(attributes);

        productService.update(products);


        return ResponseUtil.ok();

    }

    @Autowired
    CatgoryService catgoryService;
    @RequestMapping("/list")
    public Object getList(@RequestParam(value = "page",required = false,defaultValue = "1") Integer page,
                          @RequestParam(value = "limit",required = false,defaultValue = "3") Integer limit,
                          @RequestParam(value = "sort",required = false,defaultValue = "add_time") String sort,
                          @RequestParam(value = "order",required = false,defaultValue = "desc") String order,
                          String number,String name){
        Subject subject = SecurityUtils.getSubject();
        DtsAdmin admin = (DtsAdmin) subject.getPrincipal();

        List<Integer> brandsIds = null;

        if(subject.hasRole("品牌制造商")){
            brandsIds = brandService.findByid(admin.getId());
        }

        Map<String,Object> map = goodsService.getPage(page,limit,sort,order,number,name,brandsIds);

        return ResponseUtil.ok(map);
    }

    @RequestMapping("/catAndBrand")
    public Object getCatAndBrand(){

        List<CatVo> list = catgoryService.selectlist();

        List<Map<String,Object>> list1 = brandService.selectAll();

        Map<String,Object> map = new HashMap<>();

        map.put("categoryList",list);
        map.put("brandList",list1);
        return ResponseUtil.ok(map);

    }

    @RequestMapping("/create")
    public Object create(@RequestBody GoodsAllinone goodsAllinone){

        Object result = check(goodsAllinone);
        if(result!=null){
            return result;

        }
        try {
            goodsService.insert(goodsAllinone);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseUtil.fail(-1,e.getMessage());
        }



        return ResponseUtil.ok();
    }

    private Object check(GoodsAllinone goodsAllinone) {
        DtsGoods goods = goodsAllinone.getGoods();
        if(goods ==null || StringUtils.isEmpty(goods.getName())||StringUtils.isEmpty(goods.getGoodsSn())){
            return ResponseUtil.fail(-1,"商品号和编号不能为空");
        }
        DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
        if(attributes!= null && attributes.length > 0){
            for (DtsGoodsAttribute attribute : attributes) {
                if(StringUtils.isEmpty(attribute.getAttribute())){
                    return ResponseUtil.fail(-1, "商品属性名称不能为空");
                }
            }
        }
        DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();

        if(specifications!= null && specifications.length > 0){
            for (DtsGoodsSpecification specification : specifications) {
                if(StringUtils.isEmpty(specification.getSpecification())){
                    return ResponseUtil.fail(-1, "商品规格名称不能为空");
                }
            }
        }
        DtsGoodsProduct[] products = goodsAllinone.getProducts();

        if(products== null || products.length == 0){
            return ResponseUtil.fail(-1, "商品库存不能为空");
        }


        //判断 品牌商
        Integer brandId = goods.getBrandId();
        if(brandId != null){
            DtsBrand brand = brandService.findById(brandId);
            if(brand == null){
                return ResponseUtil.fail(-1, "商品品牌不存在");
            }
        }


        //判断分类

        Integer categoryId = goods.getCategoryId();
        if(categoryId != null){
            DtsCategory category = catgoryService.findById(categoryId);
            if(category == null){
                return ResponseUtil.fail(-1, "商品分类不存在");
            }
        }


        return null;
    }

    @RequestMapping("/detail")
    public Object getDetail(@RequestParam("id") Integer goodsId){

       DtsGoods goods =  goodsService.selectById(goodsId);

       Integer categoryId = goods.getCategoryId();

       Integer pid = catgoryService.selectId(categoryId);

       List<Integer> list = new ArrayList<>();
       list.add(pid);
       list.add(categoryId);

       List<DtsGoodsAttribute> list1 = attributeService.select(goodsId);

       List<DtsGoodsSpecification> specifications = specificationService.select(goodsId);

       List<DtsGoodsProduct> dtsGoodsProducts = productService.select(goodsId);

       Map<String,Object> data = new HashMap<>();

       data.put("categoryIds",list);
       data.put("goods",goods);
       data.put("attributes",list1);
       data.put("specifications",specifications);
       data.put("products",dtsGoodsProducts);

       return ResponseUtil.ok(data);


    }




















}
